package cn.jbolt.core.kit;

import cn.jbolt.core.cache.JBoltDeptCache;
import cn.jbolt.core.cache.JBoltOnlineUserCache;
import cn.jbolt.core.cache.JBoltRoleCache;
import cn.jbolt.core.cache.JBoltUserCache;
import cn.jbolt.core.model.OnlineUser;
import cn.jbolt.core.model.User;
import com.alibaba.ttl.TransmittableThreadLocal;
import com.jfinal.kit.StrKit;

import java.util.List;

/**
 * JBolt平台后端用户登录ThreadLocal相关
 *
 * @ClassName: JBoltUserKit
 * @author: JFinal学院-小木 QQ：909854136
 * @date: 2021年2月21日
 */
public class JBoltUserKit {

	/**
	 * 针对JBolt平台登录用户SessionId的ThreadLocal
	 */
	private static final TransmittableThreadLocal<String> jboltSessionIdTL = new TransmittableThreadLocal<String>();
	/**
	 * 针对JBolt平台登录用户UserId的ThreadLocal
	 */
	private static final TransmittableThreadLocal<Long> jboltUserTL = new TransmittableThreadLocal<Long>();
	/**
	 * 存是否锁屏了
	 */
	private static final TransmittableThreadLocal<Boolean> jboltUserScreenLockedTL = new TransmittableThreadLocal<Boolean>();

	/**
	 * 删除用户ID
	 */
	public static void removeUserId() {
		jboltUserTL.remove();
	}

	/**
	 * 获取当前用户
	 *
	 * @return
	 */
	public static User getUser() {
		return JBoltUserCache.me.get(getUserId());
	}

	/**
	 * 获得当前访问的userId
	 *
	 * @return
	 */
	public static Long getUserId() {
		return jboltUserTL.get();
	}

	/**
	 * 获得当前访问的用户的部门ID
	 *
	 * @return
	 */
	public static Long getUserDeptId() {
		return JBoltUserCache.me.getDeptId(getUserId());
	}

	/**
	 * 获得当前访问的用户的所属模块
	 *
	 * @return
	 */
	public static Integer getUserOfModule() {
		return JBoltUserCache.me.getOfModule(getUserId());
	}

	/**
	 * 判断当前用户是否为系统默认后台管理平台内置用户
	 * @return
	 */
	public static boolean isPlatformInnerAdminUser() {
		return JBoltUserCache.me.isPlatformInnerAdminUser(getUserId());
	}

	/**
	 * 获得当前访问的用户的所属模块 关联内容
	 *
	 * @return
	 */
	public static String getUserOfMoudleLink() {
		return JBoltUserCache.me.getOfModuleLink(getUserId());
	}

	/**
	 * 获得当前访问的用户的部门sn
	 *
	 * @return
	 */
	public static String getUserDeptSn() {
		User user = getUser();
		return user == null ? null : JBoltDeptCache.me.getSn(user.getDeptId());
	}

	/**
	 * 获得当前访问的用户的部门name
	 *
	 * @return
	 */
	public static String getUserDeptName() {
		User user = getUser();
		return user == null ? null : JBoltDeptCache.me.getName(user.getDeptId());
	}

	/**
	 * 设置当前用户ID
	 *
	 * @param userId
	 */
	public static void setUserId(Long userId) {
		jboltUserTL.set(userId);
	}

	/**
	 * 获得当前访问的用户name
	 *
	 * @return
	 */
	public static String getUserName() {
		User user = getUser();
		return user == null ? null : user.getName();
	}

	/**
	 * 获得当前访问的用户头像
	 *
	 * @return
	 */
	public static String getUserAvatar() {
		User user = getUser();
		return user == null ? null : user.getAvatar();
	}

	/**
	 * 获得当前访问的用户登录账号用户名username
	 *
	 * @return
	 */
	public static String getUserUserName() {
		User user = getUser();
		return user == null ? null : user.getUsername();
	}

	/**
	 * 获得当前访问的用户roles字段值
	 *
	 * @return
	 */
	public static String getUserRoleIds() {
		User user = getUser();
		return user == null ? null : user.getRoles();
	}

	/**
	 * 获得当前访问的用户角色对应的全部sn 连接字符串
	 *
	 * @return
	 */
	public static String getUserRoleSns() {
		String roleIds = getUserRoleIds();
		return StrKit.isBlank(roleIds)?null: JBoltRoleCache.me.getSnStrByIds(roleIds);
	}

	/**
	 * 获得当前访问的用户角色对应的全部sn list
	 *
	 * @return
	 */
	public static List<String> getUserRoleSnList() {
		String roleIds = getUserRoleIds();
		return StrKit.isBlank(roleIds)?null: JBoltRoleCache.me.getSnListByIds(roleIds);
	}

	/**
	 * 判断是否为超管
	 *
	 * @return
	 */
	public static boolean isSystemAdmin() {
		User user = getUser();
		if (user == null) {
			return false;
		}
		return user.getIsSystemAdmin();
	}

	/**
	 * 判断是否为有效可用用户
	 *
	 * @return
	 */
	public static boolean isEnable() {
		User user = getUser();
		if (user == null) {
			return false;
		}
		return user.getEnable();
	}

	/**
	 * 设置当前用户sessionId
	 *
	 * @param sessionId
	 */
	public static void setUserSessionId(String sessionId) {
		jboltSessionIdTL.set(sessionId);
	}

	/**
	 * 获取当前用户sessionId
	 *
	 * @return
	 */
	public static String getUserSessionId() {
		return jboltSessionIdTL.get();
	}

	/**
	 * 删除当前用户的sessionId
	 */
	public static void removeUserSessionId() {
		jboltSessionIdTL.remove();
	}

	/**
	 * 获取当前onlineuser 信息
	 */
	public static OnlineUser getOnlineUser() {
		return JBoltOnlineUserCache.me.getBySessionId(getUserSessionId());
	}

	/**
	 * 清空threadLocal
	 */
	public static void clear() {
		removeUserId();
		removeUserSessionId();
		removeScreenLocked();
	}

	/**
	 * 设置用户锁屏
	 */
	public static void setScreenIsLocked() {
		jboltUserScreenLockedTL.set(true);
	}

	/**
	 * 设置用户锁屏
	 */
	public static boolean userScreenIsLocked() {
		Boolean result = jboltUserScreenLockedTL.get();
		return result != null && result;
	}

	/**
	 * 清除用户锁屏
	 */
	public static void removeScreenLocked() {
		jboltUserScreenLockedTL.remove();
	}

}
